package com.myteam.mystory.DAO;

import java.util.ArrayList;

import com.myteam.mystory.entity.ChapterEntity;
import com.myteam.mystory.entity.ParamEntity;
import com.myteam.mystory.utils.LogUtils;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ParamDAO {
	public static final String TAG = "ParamDAO";
	private final SQLiteDatabase db;
	
	public ParamDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_chapter(ParamEntity paramEntity){
		ContentValues value = new ContentValues();
		value.put(ParamScheme.CODE, paramEntity.getCode());
		value.put(ParamScheme.VALUE, paramEntity.getValue());
		value.put(ParamScheme.TYPE, paramEntity.getType());
		value.put(ParamScheme.STATUT, paramEntity.getStatut());
		if (db.insert(ParamScheme.TABLE_PARAM_NAME, null, value) == -1) {
			LogUtils.e("Bug insert param");
		} else {
			LogUtils.e("successfully insert param");
		}
		return 0;
	}
	
	public ArrayList<ParamEntity> getAllParam() {
		ArrayList<ParamEntity> list_param = new ArrayList<ParamEntity>();
		String query = "select * from " + ParamScheme.TABLE_PARAM_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()){
			do {
				ParamEntity paramEntity = new ParamEntity();
				paramEntity.setCode(cursor.getString(0));
				paramEntity.setValue(cursor.getString(1));
				paramEntity.setType(cursor.getString(2));
				paramEntity.setStatut(cursor.getString(3));
				list_param.add(paramEntity);
			} while (cursor.moveToNext());
		}
		return list_param;
	}
	
	public long deleteTableParam(){
		long result = 0;
		try {
			result = db.delete(ParamScheme.TABLE_PARAM_NAME, null, null);
			LogUtils.e("delete table param successfully");
		} catch (Exception e) {
			LogUtils.e("delete table param successfully");
		}
		return result;
	}
}
